﻿using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HostAssistant
{
    public partial class addftp : U1Form
    {
        public addftp()
        {
            InitializeComponent();
            lblhid.Visible = false;
            checkEdit1.Properties.RadioGroupIndex = 1;
            checkEdit2.Properties.RadioGroupIndex = 1;
            txtkj.ReadOnly = true;
           
        }
        #region 添加mssql

        private void simpleButton1_Click(object sender, EventArgs e)
        {
          
            try
            {

                if (String.IsNullOrWhiteSpace(txtuser.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("FTP名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (String.IsNullOrWhiteSpace(txtpwd.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("FTP密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (String.IsNullOrWhiteSpace(txtkj.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("存放目录不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }


               if(!checkEdit1.Checked  && !checkEdit2.Checked)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("操作权限不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
               
                
                OpHelper.CallWithTimeout(Addmssql, 5000);

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                
                    splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (lblhid.Text == "1")
                {
                    lblhid.Text = "0";
                    Form1 f1 = (Form1)this.Owner;
                    f1.op("FTP列表");
                    this.Dispose();
                }
              
            }

        }


         void Addmssql()
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            
            string ftpname = this.txtuser.Text.Trim().ToString();
            string ftppass = this.txtpwd.Text.Trim().ToString();
            string ftppath = this.txtkj.Text;
           
          
              
            
                    try
                    {
                        if (!System.IO.Directory.Exists(ftppath))
                        {
                            System.IO.Directory.CreateDirectory(ftppath);
                        }
                    }
                    catch (Exception ex)
                    {
                      NLogger.Error(ex.ToString());
                    }
                    bool rw = false;
            if (checkEdit2.Checked)
                rw = true;  
                    IISManager iismanager = new IISManager();
                    int errorCode = iismanager.CreateFtp(rw, ftpname, ftppass, ftppath);
                    if (errorCode == ErrorCode.Succeed)
                    {

                string insert_sql = "insert into Ftpsite(Name,Pwd,OperPower,Path,CreTime,iisversion) values(@Name,@Pwd,@OperPower,@Path,@CreTime,@iisversion)";       //插入的SQL语句(带参数)
                Ftpsite Ftpsite = new Ftpsite();


             
                    //    Ftpsite.ftproot = values[i];

                Ftpsite.Name = ftpname;



                Ftpsite.Pwd = ftppass;



                Ftpsite.Path = ftppath;



                     //   Ftpsite.iisversion = values[i];


            
                Ftpsite.CreTime = DateTime.Now.ToString();

                Ftpsite.OperPower = (rw == true ? "读写" : "只读") ;
                Data<Ftpsite>.GetInstance().Add(insert_sql, Ftpsite);







                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("成功创建FTP", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
             


            }
                    else if (errorCode == ErrorCode.ftpExists)
                    {
                      

                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("FTP站点已经存在请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }
                    else if (errorCode == ErrorCode.ftpSiteFail)
                    {
                       

                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("创建FTP失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
                    else if (errorCode == ErrorCode.UserNotAdd)
                    {
                      


                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("创建FTP失败,密码不满足密码策略的要求!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            lblhid.Text = "1";
        }

        #endregion

        #region 返回
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

      

      
        /// <summary>
        /// 请选择文件路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtkj.Text = foldPath;


            }
        }

        /// <summary>
        /// 随机密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            txtpwd.Text = Guid.NewGuid().ToString().Substring(0,6);
        }

        /// <summary>
        /// 用户名只能为字母、数字和下划线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtuser_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.txtuser.ImeMode = ImeMode.Off;

            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') ||
                  (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar == 8) || (e.KeyChar == '_'))
            {
                e.Handled = false;
            }

            else
            {
              //  MessageBox.Show("用户名只能为字母、数字和下划线！");
                DevExpress.XtraEditors.XtraMessageBox.Show("用户名只能为字母、数字和下划线！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                e.Handled = true;
            }
        }

       
    }
}
